﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Pixysoft.Framework.Verifications
{
    /// <summary>
    /// http://blog.sobnb.com/u/92/20081913091954.html
    /// decimal关键字表示128位数据类型。同浮点型相比，decimal类型具有更高的精度和更小的范围，这使它适合于财务和货币计算。该类型的存储值如表4-3所示。
    /// http://blog.csdn.net/zjfei/archive/2009/04/20/4094220.aspx
    /// </summary>
    class TrueVerifior : IVerifior
    {
        object target = null;

        string message = null;

        private TrueVerifior()
        {
        }

        internal TrueVerifior(object value)
        {
            target = value;
        }



        public IVerifior IsNullOrEmpty()
        {
            if (target == null)
                return this;

            if (string.IsNullOrEmpty(target.ToString()))
                return this;

            return new FalseVerifior("待验证参数非空");
        }

        public IVerifior IsNotNullOrEmpty()
        {
            if (IsNullOrEmpty().Result())
            {
                return new FalseVerifior("待验证参数为空");
            }

            return this;
        }




        public IVerifior IsInt()
        {
            if (!IsNotNullOrEmpty().Result(out message))
                return new FalseVerifior(message);

            int result = -1;

            if (!int.TryParse(target.ToString(), out result))
                return new FalseVerifior("待验证参数非整数");

            return this;
        }

        public IVerifior IsDouble()
        {
            if (!IsNotNullOrEmpty().Result(out message))
                return new FalseVerifior(message);

            double result = -1;

            if (!double.TryParse(target.ToString(), out result))
                return new FalseVerifior("待验证参数非双精度浮点数字");

            return this;
        }

        public IVerifior IsDateTime()
        {
            if (!IsNotNullOrEmpty().Result(out message))
                return new FalseVerifior(message);

            DateTime result;

            if (!DateTime.TryParse(target.ToString(), out result))
                return new FalseVerifior("待验证参数非日期");

            return this;
        }

        public IVerifior IsShort()
        {
            if (!IsNotNullOrEmpty().Result(out message))
                return new FalseVerifior(message);

            short result;

            if (!short.TryParse(target.ToString(), out result))
                return new FalseVerifior("待验证参数非16位带符号整数");

            return this;
        }

        public IVerifior IsLong()
        {
            if (!IsNotNullOrEmpty().Result(out message))
                return new FalseVerifior(message);

            long result;

            if (!long.TryParse(target.ToString(), out result))
                return new FalseVerifior("待验证参数非64位带符号整数");

            return this;
        }

        public IVerifior IsDecimal()
        {
            if (!IsNotNullOrEmpty().Result(out message))
                return new FalseVerifior(message);

            decimal result;

            if (!decimal.TryParse(target.ToString(), out result))
                return new FalseVerifior("待验证参数非128位数据");

            return this;
        }

        public IVerifior IsFloat()
        {
            if (!IsNotNullOrEmpty().Result(out message))
                return new FalseVerifior(message);

            float result;

            if (!float.TryParse(target.ToString(), out result))
                return new FalseVerifior("待验证参数非单精度浮点数字");

            return this;
        }




        public IVerifior IsGreater(int value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsInt().Result(out message))
                return new FalseVerifior(message);

            if (int.Parse(target.ToString()) > value)
                return this;
            else
                return new FalseVerifior("待验证参数小于等于{0}", value);
        }

        public IVerifior IsGreater(double value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDouble().Result(out message)) return new FalseVerifior(message);

            if (double.Parse(target.ToString()) > value)
                return this;
            else
                return new FalseVerifior("待验证参数小于等于{0}", value);
        }

        public IVerifior IsGreater(DateTime value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDateTime().Result(out message)) return new FalseVerifior(message);

            if (DateTime.Parse(target.ToString()) > value)
                return this;
            else
                return new FalseVerifior("待验证参数小于等于{0}", value);
        }

        public IVerifior IsGreater(short value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsShort().Result(out message)) return new FalseVerifior(message);

            if (double.Parse(target.ToString()) > value)
                return this;
            else
                return new FalseVerifior("待验证参数小于等于{0}", value);
        }

        public IVerifior IsGreater(long value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDouble().Result(out message)) return new FalseVerifior(message);

            if (double.Parse(target.ToString()) > value)
                return this;
            else
                return new FalseVerifior("待验证参数小于等于{0}", value);
        }

        public IVerifior IsGreater(decimal value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDouble().Result(out message)) return new FalseVerifior(message);

            if (decimal.Parse(target.ToString()) > value)
                return this;
            else
                return new FalseVerifior("待验证参数小于等于{0}", value);
        }

        public IVerifior IsGreater(float value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDouble().Result(out message)) return new FalseVerifior(message);

            if (double.Parse(target.ToString()) > value)
                return this;
            else
                return new FalseVerifior("待验证参数小于等于{0}", value);
        }

        public IVerifior IsGreater<T>(T value)
        {
            Type valuetype = typeof(T);

            if (valuetype.Equals(typeof(int)))
                return IsGreater(int.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(double)))
                return IsGreater(double.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(DateTime)))
                return IsGreater(DateTime.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(short)))
                return IsGreater(short.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(long)))
                return IsGreater(long.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(decimal)))
                return IsGreater(decimal.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(float)))
                return IsGreater(float.Parse(value.ToString()));

            return new FalseVerifior("待验证参数数据类型不满足数值要求");
        }




        public IVerifior IsLess(int value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsInt().Result(out message))
                return new FalseVerifior(message);

            if (int.Parse(target.ToString()) < value)
                return this;
            else
                return new FalseVerifior("待验证参数大于等于{0}", value);
        }

        public IVerifior IsLess(double value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDouble().Result(out message)) return new FalseVerifior(message);

            if (double.Parse(target.ToString()) < value)
                return this;
            else
                return new FalseVerifior("待验证参数大于等于{0}", value);
        }

        public IVerifior IsLess(DateTime value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDateTime().Result(out message)) return new FalseVerifior(message);

            if (DateTime.Parse(target.ToString()) < value)
                return this;
            else
                return new FalseVerifior("待验证参数大于等于{0}", value);
        }

        public IVerifior IsLess(short value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsShort().Result(out message)) return new FalseVerifior(message);

            if (short.Parse(target.ToString()) < value)
                return this;
            else
                return new FalseVerifior("待验证参数大于等于{0}", value);
        }

        public IVerifior IsLess(long value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsLong().Result(out message)) return new FalseVerifior(message);

            if (long.Parse(target.ToString()) < value)
                return this;
            else
                return new FalseVerifior("待验证参数大于等于{0}", value);
        }

        public IVerifior IsLess(decimal value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDecimal().Result(out message)) return new FalseVerifior(message);

            if (decimal.Parse(target.ToString()) < value)
                return this;
            else
                return new FalseVerifior("待验证参数大于等于{0}", value);
        }

        public IVerifior IsLess(float value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsFloat().Result(out message)) return new FalseVerifior(message);

            if (float.Parse(target.ToString()) < value)
                return this;
            else
                return new FalseVerifior("待验证参数大于等于{0}", value);
        }

        public IVerifior IsLess<T>(T value)
        {
            Type valuetype = typeof(T);

            if (valuetype.Equals(typeof(int)))
                return IsLess(int.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(double)))
                return IsLess(double.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(DateTime)))
                return IsLess(DateTime.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(short)))
                return IsLess(short.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(long)))
                return IsLess(long.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(decimal)))
                return IsLess(decimal.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(float)))
                return IsLess(float.Parse(value.ToString()));

            return new FalseVerifior("待验证参数数据类型不满足数值要求");
        }



        public IVerifior IsGreaterEqual(int value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsInt().Result(out message))
                return new FalseVerifior(message);

            if (int.Parse(target.ToString()) >= value)
                return this;
            else
                return new FalseVerifior("待验证参数小于{0}", value);
        }

        public IVerifior IsGreaterEqual(double value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDouble().Result(out message)) return new FalseVerifior(message);

            if (double.Parse(target.ToString()) >= value)
                return this;
            else
                return new FalseVerifior("待验证参数小于{0}", value);
        }

        public IVerifior IsGreaterEqual(DateTime value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDateTime().Result(out message)) return new FalseVerifior(message);

            if (DateTime.Parse(target.ToString()) >= value)
                return this;
            else
                return new FalseVerifior("待验证参数小于{0}", value);
        }

        public IVerifior IsGreaterEqual(short value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsShort().Result(out message)) return new FalseVerifior(message);

            if (short.Parse(target.ToString()) >= value)
                return this;
            else
                return new FalseVerifior("待验证参数小于{0}", value);
        }

        public IVerifior IsGreaterEqual(long value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsLong().Result(out message)) return new FalseVerifior(message);

            if (long.Parse(target.ToString()) >= value)
                return this;
            else
                return new FalseVerifior("待验证参数小于{0}", value);
        }

        public IVerifior IsGreaterEqual(decimal value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDecimal().Result(out message)) return new FalseVerifior(message);

            if (decimal.Parse(target.ToString()) >= value)
                return this;
            else
                return new FalseVerifior("待验证参数小于{0}", value);
        }

        public IVerifior IsGreaterEqual(float value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsFloat().Result(out message)) return new FalseVerifior(message);

            if (float.Parse(target.ToString()) >= value)
                return this;
            else
                return new FalseVerifior("待验证参数小于{0}", value);
        }

        public IVerifior IsGreaterEqual<T>(T value)
        {
            Type valuetype = typeof(T);

            if (valuetype.Equals(typeof(int)))
                return IsGreaterEqual(int.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(double)))
                return IsGreaterEqual(double.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(DateTime)))
                return IsGreaterEqual(DateTime.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(short)))
                return IsGreaterEqual(short.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(long)))
                return IsGreaterEqual(long.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(decimal)))
                return IsGreaterEqual(decimal.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(float)))
                return IsGreaterEqual(float.Parse(value.ToString()));

            return new FalseVerifior("待验证参数数据类型不满足数值要求");
        }



        public IVerifior IsLessEqual(int value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsInt().Result(out message))
                return new FalseVerifior(message);

            if (int.Parse(target.ToString()) <= value)
                return this;
            else
                return new FalseVerifior("待验证参数大于{0}", value);
        }

        public IVerifior IsLessEqual(double value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDouble().Result(out message))
                return new FalseVerifior(message);

            if (double.Parse(target.ToString()) <= value)
                return this;
            else
                return new FalseVerifior("待验证参数大于{0}", value);
        }

        public IVerifior IsLessEqual(DateTime value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDateTime().Result(out message))
                return new FalseVerifior(message);

            if (DateTime.Parse(target.ToString()) <= value)
                return this;
            else
                return new FalseVerifior("待验证参数大于{0}", value);
        }

        public IVerifior IsLessEqual(short value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsShort().Result(out message))
                return new FalseVerifior(message);

            if (short.Parse(target.ToString()) <= value)
                return this;
            else
                return new FalseVerifior("待验证参数大于{0}", value);
        }

        public IVerifior IsLessEqual(long value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsLong().Result(out message))
                return new FalseVerifior(message);

            if (long.Parse(target.ToString()) <= value)
                return this;
            else
                return new FalseVerifior("待验证参数大于{0}", value);
        }

        public IVerifior IsLessEqual(decimal value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDecimal().Result(out message))
                return new FalseVerifior(message);

            if (decimal.Parse(target.ToString()) <= value)
                return this;
            else
                return new FalseVerifior("待验证参数大于{0}", value);
        }

        public IVerifior IsLessEqual(float value)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsFloat().Result(out message))
                return new FalseVerifior(message);

            if (float.Parse(target.ToString()) <= value)
                return this;
            else
                return new FalseVerifior("待验证参数大于{0}", value);
        }

        public IVerifior IsLessEqual<T>(T value)
        {
            Type valuetype = typeof(T);

            if (valuetype.Equals(typeof(int)))
                return IsLessEqual(int.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(double)))
                return IsLessEqual(double.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(DateTime)))
                return IsLessEqual(DateTime.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(short)))
                return IsLessEqual(short.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(long)))
                return IsLessEqual(long.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(decimal)))
                return IsLessEqual(decimal.Parse(value.ToString()));
            else if (valuetype.Equals(typeof(float)))
                return IsLessEqual(float.Parse(value.ToString()));

            return new FalseVerifior("待验证参数数据类型不满足数值要求");
        }


        public IVerifior IsEqual(object value)
        {
            return IsEqual(value, false);
        }

        public IVerifior IsEqual(object value, bool ignoreCase)
        {
            if (!IsNotNullOrEmpty().Result(out message))
                return new FalseVerifior(message);

            if (value == null)
                return new FalseVerifior("输入的参数为空");

            if (string.IsNullOrEmpty(value.ToString()))
                return new FalseVerifior("输入的参数为空");

            if (ignoreCase)
            {
                string mvalue = this.target.ToString().ToUpper().Trim();

                string tvalue = value.ToString().Trim().ToUpper();

                if (tvalue == mvalue)
                    return this;
                else
                    return new FalseVerifior("不相等");
            }
            else
            {
                string mvalue = this.target.ToString().Trim();

                string tvalue = value.ToString().Trim();

                if (tvalue == mvalue)
                    return this;
                else
                    return new FalseVerifior("不相等");

            }
        }



        public IVerifior IsChinese()
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (Regex.IsMatch(target.ToString(), @"[\u4e00-\u9fa5]+"))
            {
                return this;
            }
            else
            {
                return new FalseVerifior("待验证参数非中文");
            }
        }

        public IVerifior IsEmail()
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (RegexCoder.Instance.IsEmail(target.ToString()))
            {
                return this;
            }
            else
            {
                return new FalseVerifior("待验证参数非电子邮件");
            }
        }

        public IVerifior IsUrl()
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (RegexCoder.Instance.IsUrl(target.ToString()))
            {
                return this;
            }
            else
            {
                return new FalseVerifior("待验证参数非URL");
            }
        }

        public IVerifior IsIP()
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (RegexCoder.Instance.IsIP(target.ToString()))
            {
                return this;
            }
            else
            {
                return new FalseVerifior("待验证参数非IP");
            }
        }

        public IVerifior IsTel()
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (RegexCoder.Instance.IsFixedPhone(target.ToString()))
            {
                return this;
            }
            else
            {
                return new FalseVerifior("待验证参数非固话");
            }
        }

        public IVerifior IsMobile()
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (RegexCoder.Instance.IsCellPhone(target.ToString()))
            {
                return this;
            }
            else
            {
                return new FalseVerifior("待验证参数非移动电话号码");
            }
        }

        public IVerifior IsPostCode()
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (RegexCoder.Instance.IsPostCode(target.ToString()))
            {
                return this;
            }
            else
            {
                return new FalseVerifior("待验证参数非邮政编码");
            }
        }

        public IVerifior IsQQ()
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (!IsDouble().Result(out message)) return new FalseVerifior(message);

            if (Regex.IsMatch(target.ToString(), "[1-9][0-9]{4,}", RegexOptions.IgnoreCase))
            {
                return this;
            }
            else
            {
                return new FalseVerifior("待验证参数非QQ");
            }
        }

        public IVerifior IsIDCard()
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (RegexCoder.Instance.IsIDCard(target.ToString()))
            {
                return this;
            }
            else
            {
                return new FalseVerifior("待验证参数非身份证号码");
            }
        }

        public IVerifior IsInLengthRange(int min, int max)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            int length = System.Text.Encoding.GetEncoding("gb2312").GetBytes(target.ToString()).Length;

            if (length > max || length < min)
            {
                return new FalseVerifior("待验证参数长度不符合要求，其必须大于{0}，小于{1}", min, max);
            }
            else
            {
                return this;
            }
        }

        public IVerifior IsLength(int length)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            int _length = System.Text.Encoding.GetEncoding("gb2312").GetBytes(target.ToString()).Length;

            if (_length != length)
            {
                return new FalseVerifior("待验证参数长度等于输入要求");
            }
            else
            {
                return this;
            }
        }


        public IVerifior IsMatchRegex(string regex)
        {
            if (!IsNotNullOrEmpty().Result(out message)) return new FalseVerifior(message);

            if (Regex.IsMatch(target.ToString(), regex))
            {
                return this;
            }
            else
            {
                return new FalseVerifior("待验证参数不满足正则表达式");
            }
        }



        public bool Result()
        {
            return true;
        }

        bool IVerifior.Result(out string message)
        {
            message = this.message;

            return true;
        }

        private void test()
        {
            string pwd1 = "ABC";

            string pwd2 = "abc";

            bool result = Verification.Verify(pwd1).IsEqual(pwd2, true).IsLength(2).Result();

            Console.Write(result);
        }
    }

}
